use strict;

opendir(ROOTDIR, ".");
my @locales = grep { !/^\.\.?$/ && !/^en-US$/ } readdir(ROOTDIR);
closedir(ROOTDIR);

#print join("\n", @locales), "\n";

opendir(BASEDIR, "en-US");
my @basefiles = grep { !/^\.\.?$/ } readdir(BASEDIR);
closedir(BASEDIR);

#print join("\n", @basefiles), "\n";


my %rowsmap = ();
foreach my $basefile (@basefiles) {
	open(BASEFILE, "<en-US/$basefile");
	
	while (<BASEFILE>) {
		if (!/^\s*$/) {
			$rowsmap{$basefile}{$_} = 1;
		}
	}
	
	my @k = keys %{$rowsmap{$basefile}};
	print "en-US, $basefile: ", scalar @k, " strings.\n";
	
	close(BASEFILE);
}

foreach my $locale (@locales) {
	foreach my $basefile (@basefiles) {
		print "$locale, $basefile: ";
		
		open(LOCALEFILE1, "<$locale/$basefile");
		my @rows = <LOCALEFILE1>;
		close(LOCALEFILE1);

		my @matchlist = grep { exists $rowsmap{$basefile}{$_} } @rows;
		
		print scalar @matchlist, " of ", scalar @rows, " matching.\n";
		
		if (@matchlist) {
			open(LOCALEFILE2, ">$locale/$basefile");
			
			foreach my $row (@rows) {
				if (!$rowsmap{$basefile}{$row}) {
					print LOCALEFILE2 $row;
				}
			}

			close(LOCALEFILE2);
		}
	}
}